import { createRouter, createWebHashHistory } from 'vue-router'
import useMainStore from '../store/main/main'
import { storeToRefs } from 'pinia'
/** 默认布局 */
const Layout = () => import('../layout/index.vue')
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/machine_sm',
      children: [
        {
          path: '/machine_sm',
          name: 'machine_sm',
          component: () => import('../views/machine_sm/index.vue')
        },
        {
          path: '/machine_yb',
          name: 'machine_yb',
          component: () => import('../views/machine_yb/index.vue')
        },
        {
          path: '/area',
          name: 'area',
          component: () => import('../views/area/index.vue')
        },
        {
          path: '/handle',
          name: 'handle',
          meta: { requirePassword: true },
          component: () => import('../views/handle/index.vue')
        },
        {
          path: '/robot',
          name: 'robot',
          component: () => import('../views/robot/index.vue')
        }
      ]
    }
  ]
})
router.beforeEach(async (to, from, next) => {
  const mainStore = useMainStore()
  mainStore.getUserInfo()
  const { authenticated, userInfo } = mainStore
  window.globalNext = next // 存储 next 函数
  if (to.meta.requirePassword && !authenticated && userInfo.status == 1) {
    mainStore.setShowForm(true) // 显示表单
    next(false)
  } else {
    mainStore.setShowForm(false) // 隐藏表单
    mainStore.setAuthenticated(false)
    next() // 允许导航继续
  }
})
export default router
